* Primary data from 2012 CCES Texas Tech University team content
*NOTES:  Variable countypop is from county population data for 2013 available on from the U.S. Census Bureau.  Data was merged using county FIPS codes.  
* NOTES: Variable rural is from the United States Department of Agriculture Rural-Urban Continuum Codes. Data was merged using Zip Codes.  Data and codes are available at: http://www.ers.usda.gov/data-products/rural-urban-continuum-codes/.aspx
* NOTES:  Correct identification of judges was from a survey of state website and ballots listing local judges and candidates on ballots. 

* Create main variables

* Create political knowledge measure

gen know1=1 if TTU304X==3
drop know1
gen know1=0
replace know1=1 if TTU304X==3
tab know1
tab TTU304X
gen know2=0
replace know2=1 if TTU305X==2
tab know2
tab TTU305X
gen know3=0
replace know3=1 if TTU306X==2
tab know3
tab TTU306X
gen know4=0
replace know4=1 if TTU307X==1
tab know4
tab TTU307X
tab TTU308X
gen know5=0
replace know5=1 if TTU308X==2
tab know5
gen POL_SOPH = know1 + know2 + know3 + know4 + know5
tab POL_SOPH

dog

* generate length of residency from CCES

gen reside=CC351

tab reside
tab CC351

Dog

* recode reside

recode reside 1/3 = 1
recode reside 4=2
recode reside 5=3
recode reside 6=4

* check work

tab reside
tab CC351

Dog

*label reside

label var reside "Length of Residency"
label define reside1 1 "Less than a year" 2 "1 to 2 years" 3 " 3 to 4 years" 4 "5 or more years"
label values reside reside1

* check coding 
tab reside
tab CC351

Dog

			* Create a dichotomous variable for correct identification of judges in pre-election.  This variable is created from the Name Pre-election measure

gen pre_election = NAME_PRE

tab pre_election
tab NAME_PRE



* create dichotomous variable no name given and wrong name given = 0.  Code correct name and Correct name but retired = 1 (seven observation represent retired)

recode pre_election 1=0
recode pre_election 2/3=1

tab pre_election



* confirm recode
tab NAME_PRE
tab pre_election

Dog

* label variable and values

label variable pre_election "Pre-election Identification"
label define pre1 0 "No or Incorrect ID" 1 "Correct ID"
label values pre_election pre1

* confirm recode

tab pre_election
tab NAME_PRE
Dog

		* generate a dichotomous variable for correct identification of judges in post-election
gen post_election = NAM_POST

tab post_election
tab NAM_POST


* create dichotomous variable for post-election. Combine no name give and wrong to code 0. Correct and correct retired (5 values) code 1

recode post_election 1=0
recode post_election 2/3=1

tab post_election



*confirm recode
tab NAM_POST
tab post_election

Dog

* label variables and values for post election dummy

label var post_election "Post-Election Identification"
label define post1 0 "No or Incorrect ID" 1 "Correct ID"
label values post_election post1

tab post_election
tab NAM_POST
DOG

* create dummy variables for judicial selection methods based on main selection variable "select"

gen partisan = 0
replace partisan = 1 if select==1

label var partisan "Partisan"
label def partisan1 0 "Other" 1 "partisan"
label values partisan partisan1

* confirm recode
tab partisan
tab select

dog

gen nonpartisan=0
replace nonpartisan = 1 if select==2
label var nonpart "Nonpartisan"
label def nonpartisan1 0 "Other" 1 "nonpartisan"
label values nonpart nonpartisan1
* confirm recode

tab nonparti
tab select



dog

gen retention=0
replace retention = 1 if select==3
label var retention "Retention"
label def retention1 0 "Other" 1 "retention"
label values retention retention1
* confirm recode
tab retention
tab select

dog

gen appointjudge=0
replace appointjudge=1 if select==4
label var appointjudge "Appointed"
label def appoint1 0 "Other" 1 "Appointed"
label values appointjudge appoint1

 * confirm recode
 tab appointjudge
 tab select
 
 dog
 
 * rename judicial interest variable

rename TTU325 interest



* gen rural/urban dummy Jan. 1, 2014
tab rural

gen urban = 1

replace urban = . if rural == .
replace urban = 0 if rural==4
replace urban = 0 if rural==5
replace urban = 0 if rural==6
replace urban = 0 if rural==7
replace urban = 0 if rural == 8
replace urban = 0 if rural==9


tab urban

DOG

* confirm recode

tab rural
tab urban

Dog

* label variable urban

label variable urban "Urban/Rural"

label define urban1 0 "rural" 1 "urban"

label values urban urban1

tab urban
tab rural

Dog

* gen nonurban variable.  Generate urban and nonurban to allow for direction in model

gen nonurban = 0
replace nonurban = . if rural == .
replace nonurban = 1 if rural==4
replace nonurban = 1 if rural==5
replace nonurban = 1 if rural==6
replace nonurban = 1 if rural==7
replace nonurban = 1 if rural == 8
replace nonurban = 1 if rural==9

* label variable nonurban

label variable nonurban "Rural"

label define nonurban1 0 "urban" 1 "rural"

label values nonurban nonurban1

tab nonurban

Dog
* confirm recode
tab nonurban
tab rural

dog

* gen age
	
	tab birthyr

* base on 2012

gen age = 2012 - birthyr
tab age

label variable age "Age"

Dog

* gen contested and uncontested variables

gen contested = 0 
replace contested = . if localju == .
replace contested = 1 if localju == 1

tab contested

label variable contested "contested"
label define cont1 0 "other" 1 "contested"
label values contested cont1

* confirm new variable 
tab contested
tab localju
dog

gen uncontested = 0
replace uncontested = . if localju==.
replace uncontested = 1 if localju==2

tab uncontested

label variable uncontested "Uncontested"
label define uncon1 0 "Other" 1 "Uncontested"
label values uncontested uncon1

* confirm new variable 
tab uncontested
tab localju
dog

gen notonballot = 1 if localju== 0
replace notonballot = . if localju==.
replace notonballot = 0 if localju==1
replace notonballot = 0 if localju == 2

label variable notonballot "Not on Ballot"
label define noballot1 0 "Other" 1 "Not on Ballot"
label values notonballot noballot1
* confirm new variable
tab notonballot
tab localju

dog
		* create race variables
 
 tab race
 
 
 
 gen black = 0
 replace black = . if race== .
 replace black = 1 if race == 2
 
 label var black "African-American"
 label define black1 0 "Other races" 1 "Black"
 label values black black1
 
 * check work
 
 tab black
 tab race
 
 dog
 


* create hispanic race variable
gen hispanic = 0
replace hispanic = . if race == .
replace hispanic = 1 if race == 3

label var hispanic "Hispanic"
label define hispanic1 0 "Other races" 1 "Hispanic"
label values hispanic hispanic1

* checkk work
tab hispanic
tab race

dog


* create white  race variable
gen white = 0
replace white = . if race == .
replace white = 1 if race == 1

label var white "White"
label define white1 0 "Other races" 1 "White"
label values white white1

* checkk work
tab white
tab race

dog

* create other variable to include Asian, Native American, Mixed, Other, Middle Eastern
 
 gen other = 1
 replace other = . if race == .
 replace other = 0 if race ==1
 replace other = 0 if race ==2
 replace other = 0 if race ==3
 
 label variable other "Other Races"
 label define other1 0 "white, hisp, black" 1 "Other races"
 label values other other1
 
 * check work
 
 tab race
 tab other
 
 dog
 
 * recode and rename gender just because I want to

gen female = gender
recode female 1 = 0
recode female 2 = 1

label variable female "Female"
label define female1 0 "Male" 1 "Female"
label values female female1

tab gender
tab female

dog 

* create married variable and include domestic partnerships

tab marstat

dog

gen married = 0
replace married = . if marstat == .
replace married = 1 if marstat == 1
replace married = 1 if marstat == 6

tab married

label var married "Married or Domestic Partners"
label define married1 0 "Other" 1 "Married or Domestic Partners"
label values married married1

tab marstat
tab married

Dog
* fold PID7 to create pid
  
  gen pid = PID7
  replace pid =. if PID7 == .
  replace pid = 1 if PID7 == 7
  replace pid = 2 if PID7 == 6
  replace pid = 3 if PID7 == 5
  replace pid = 5 if PID7 == 8
  
 * check work
 
  tab PID7
  tab pid
  
  dog
  
  label variable pid "Strength of Party ID"
  label define pid1 1 "Strong Partisans" 2 "Not Very Strong Partisans" 3 "Partisan Leaners" 4 "Independents" 5 "Unsure"
  label values pid pid1
  
  tab pid
  
  dog 
  
  * create interest variable
 
 tab TTU325
 gen interest = TTU325
 tab interest
 
 dog
 
 
 label var interest  "Interest"
 label define interest1 4 "No election" 3 "Not interested at all" 2 "Somewhat interested" 1 "Very interested"
 label values interest interest1
 
 tab interest 
 tab TTU325
 
 dog
  
 * recode interest variable to create ordered measure
 
 tab interest
 
 gen judinterest = interest
 replace judinterest = . if interest == .
 replace judinterest = 0 if interest == 4
 replace judinterest = 1 if interest == 3
 replace judinterest = 2 if interest == 2
 replace judinterest = 3 if interest ==1
 
 tab judinterest
 tab interest
 
 dog
 
 label var judinterest "Interest"
 label define jud1 0 "No election" 1 "Not interested" 2 "Somewhat interested" 3 "Very interested"
 label values judinterest jud1
 
 tab judinterest 
 
 dog
